<?php

class Curl
{
    protected static $user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36';

   /**
     * @brief                  get请求
     * @param $url             请求的url
     * @param array $param     请求的参数
     * @param int $timeout     超时时间
     * @param int $log       是否启用日志
     * @return mixed
     */
    public static function get($url, $param=array(), $timeout=3, $log=1)
    {
        $ch = curl_init();
        if (is_array($param)) {
            $url = $url . '?' . http_build_query($param);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        // curl_setopt($ch, CURLOPT_HEADER,0);
        // curl_setopt($ch, CURLOPT_USERAGENT, static::$user_agent);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // 允许 cURL 函数执行的最长秒数
        // curl_multi_getcontent($ch);
        $data = curl_exec($ch);
        // if ($log) {
        //     $data .= "\r\n";
        //     $data .= self::logInfo($ch, $param, $data);
        // }
        // echo curl_errno($ch);
        curl_close($ch);
        return $data;
    }
 
    /**
     * @brief                   post请求
     * @param $url              请求的url地址
     * @param array $param      请求的参数
     * @param int $log          是否启用日志
     * @return mixed
     */
    public static function post($url, $param=array(), $header=array(),$cookie=false, $timeout=3, $log=1)
    {
        $ch = curl_init();
        if (is_array($param)) {
            $urlparam = http_build_query($param);
        } else if (is_string($param)) { //json字符串
            $urlparam = $param;
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        // curl_setopt($ch, CURLOPT_HEADER,0);
        // curl_setopt($ch, CURLOPT_USERAGENT, static::$user_agent);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //设置超时时间
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回原生的（Raw）输出
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POST, 1); //POST
        curl_setopt($ch, CURLOPT_POSTFIELDS, $urlparam); //post数据
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        if($cookie){
            $cookie_jar = './cookie.txt';
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
        }
        $data = curl_exec($ch);
        // if ($log) {
        //     $data .= "\r\n";
        //     $data .= self::logInfo($ch, $param, $data);
        // }
        
        curl_close($ch);
        return $data;
    }
 
 
    /**
     * 请求信息记录日志
     * @param $ch       curl句柄
     * @param $request  请求参数
     * @param $response 响应结果
     */
    private static function logInfo($ch, $request, $response)
    {
        $info = curl_getinfo($ch);
        $resultFormat =  "耗时:[%s] 返回状态:[%s] 请求的url[%s] 请求参数:[%s] 响应结果:[%s] 大小:[%s]kb 速度:[%s]kb/s";
        $resultLogMsg =  sprintf($resultFormat, $info['total_time'], $info['http_code'], $info['url'], var_export($request,true),var_export($response,true), $info['size_download']/1024, $info['speed_download']/1024);
        return $resultLogMsg;
    }
 
}